﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Class 的摘要说明
/// </summary>
public class Class
{
    private int classid;

    public int Classid
    {
        get { return classid; }
        set { classid = value; }
    }
    private int parentid;

    public int Parentid
    {
        get { return parentid; }
        set { parentid = value; }
    }
    private string classname;

    public string Classname
    {
        get { return classname; }
        set { classname = value; }
    }


    public const string ADDCLASS = "insert into class values(@parentid,@classname)";

    public const string DELCLASS = "delete from class where classid=@classid";

    public const string UPDATECLASS = "update class set parentid=@parentid,classname=@classname, where classid=@classid";

    public const string GETALLCLASS = "select * from class";

    public const string GETCLASSBYID = "select * from class where classid=@classid";

    public const string GETCLASSNAMEBYID = "select classname from class where classid=@classid";

    public const string GETCLASSBYPID = "select * from class where parentid=@parentid";


    public DataTable GetClassByPid(int parentid)
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETCLASSBYPID, dt,
            new SqlParameter("@parentid", parentid));
        return dt;
    }

    public static string GetCnameByid(int classid)
    {
        return SqlHelper.ExecuteScalar(GETCLASSNAMEBYID,
            new SqlParameter("@classid", classid)).ToString();
    }


    public void AddClass(Class c)
    {
        SqlHelper.ExecuteNonQuery(ADDCLASS,
            new SqlParameter("@parentid", c.Parentid),
            new SqlParameter("@classname", c.Classname));
    }

    public void UpdateClass(Class c)
    {
        SqlHelper.ExecuteNonQuery(UPDATECLASS,
            new SqlParameter("@parentid", c.Parentid),
            new SqlParameter("@classname", c.Classname),
            new SqlParameter("@classid", c.Classid));
    }

    public DataTable GetAllClass()
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETALLCLASS, dt);
        return dt;
    }

    public DataTable GetClassById(int id)
    {
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable(GETCLASSBYID, dt,
            new SqlParameter("@classid", id));
        return dt;
    }

    public int GetParentClassById(int id)
    {
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        int parentid;
        SqlHelper.FillDataTable(GETCLASSBYID, dt,
            new SqlParameter("@classid", id));
        parentid = Int32.Parse(dt.Rows[0]["parentid"].ToString());
        return parentid;
    }

    public void DelClass(int id)
    {
        SqlHelper.ExecuteNonQuery(DELCLASS, new SqlParameter("@classid", id));
    }

}
